<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>031-Android存储（6）：GreenDao使用全解</title>
  
  
  <meta property="og:title" content="031-Android存储（6）：GreenDao使用全解">
  
  
  <meta property="og:url" content="http://example.com/2019/10/12/031-Android%E5%AD%98%E5%82%A8%EF%BC%886%EF%BC%89%EF%BC%9AGreenDao%E4%BD%BF%E7%94%A8%E5%85%A8%E8%A7%A3/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2019-10-12">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Android">
  
  <meta property="og:article:tag" content="Android持久化">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      031-Android存储（6）：GreenDao使用全解
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2019-10-12T04:53:21.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2019-10-12</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Android/" class="post-meta-link">Android</a>
    
    
    
    <span class="dot"></span>
    <span>2.3k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Android/" class="post-meta-link">Android</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Android持久化/" class="post-meta-link">Android持久化</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <p><a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/article/details/114267141">【5年Android从零复盘系列之三十一】Android存储（6）：GreenDao</a></p>
<hr />

<p><strong>【代码是最好的老师】</strong></p>
<h1 id="1-概述"><a href="#1-概述" class="headerlink" title="1.概述"></a>1.概述</h1><p>GreenDAO 是一个将对象映射到 SQLite 数据库中的轻量且快速的 ORM 框架</p>
<p>greendao具有以下优点，是项目架构期db方案的首选：</p>
<ol>
<li>存取速度快</li>
<li>支持加密</li>
<li>轻量、api极简</li>
<li>支持缓存</li>
<li>中间代码自动生成，代码编写量小</li>
<li>【重点】开源至今，经历千锤百炼，稳定可靠</li>
</ol>
<p><a target="_blank" rel="noopener" href="https://github.com/greenrobot/greenDAO">GitHub直达    GreenDao</a></p>
<h1 id="2-使用"><a href="#2-使用" class="headerlink" title="2.使用"></a>2.使用</h1><p><a target="_blank" rel="noopener" href="https://download.csdn.net/download/Cupster/15503301">GreenDao3.3.0Demo.zip</a></p>
<p>总体分为三步：</p>
<ol>
<li>配置导包 </li>
<li>编写实体类（设计表）</li>
<li>编写CRUD工具类</li>
<li>调用封装的CRUD方法</li>
</ol>
<p>本次采用得是组件化，使数据库有关的代码独立在一个module中，在APP module中调用。规范化开发，方便后续直接引入db_greendao库。</p>
<h2 id="2-1-配置"><a href="#2-1-配置" class="headerlink" title="2.1 配置"></a>2.1 配置</h2><p>step1、step2：</p>
<p>在项目根build.gradle中配置仓库源及插件</p>
<pre><code> //greendao_step1: add repository
 mavenCentral()
 
 //greendao_step2: add plugin
 classpath &#39;org.greenrobot:greendao-gradle-plugin:3.3.0&#39; 
</code></pre>
<p>如：</p>
<pre class="highlight"><span class="line"><span class="comment">// Top-level build file where you can add configuration options common to all sub-projects/modules.</span></span><br><span class="line"><span class="keyword">buildscript</span> &#123;</span><br><span class="line">    <span class="keyword">repositories</span> &#123;</span><br><span class="line">        google()</span><br><span class="line">        jcenter()</span><br><span class="line">        <span class="comment">//greendao_step1: add repository</span></span><br><span class="line">        mavenCentral()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">dependencies</span> &#123;</span><br><span class="line">        <span class="keyword">classpath</span> <span class="string">&quot;com.android.tools.build:gradle:4.0.2&quot;</span></span><br><span class="line">        <span class="keyword">classpath</span> <span class="string">&#x27;org.greenrobot:greendao-gradle-plugin:3.3.0&#x27;</span> <span class="comment">//greendao_step2: add plugin</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">allprojects</span> &#123;</span><br><span class="line">    <span class="keyword">repositories</span> &#123;</span><br><span class="line">        google()</span><br><span class="line">        jcenter()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">task</span> clean(type: <span class="keyword">Delete</span>) &#123;</span><br><span class="line">    <span class="keyword">delete</span> rootProject.buildDir</span><br><span class="line">&#125;</span><br></pre>


<h2 id="2-2-应用插件-amp-导包"><a href="#2-2-应用插件-amp-导包" class="headerlink" title="2.2 应用插件&amp;导包"></a>2.2 应用插件&amp;导包</h2><p>step3、step4、step5、step6:</p>
<p>gradle配置：应用插件、导包、配置greendao中间代码生成路径</p>
<pre><code>//greendao_step3: apply plugin
apply plugin: &#39;org.greenrobot.greendao&#39;

//greendao_step4: add library
implementation &#39;org.greenrobot:greendao:3.3.0&#39; 

//greendao_step5:
//GreenDao 3.0+采用注解的方式来定义实体类，通过gradle插件生成相应的代码
greendao &#123;
    schemaVersion 1
    daoPackage &#39;com.cupster.func_db_greendao.auto&#39;//建议同一包名下
    targetGenDir &#39;src/main/java&#39;
&#125;
</code></pre>
<p>如：</p>
<pre class="highlight"><span class="line">apply plugin: <span class="string">&#x27;com.android.application&#x27;</span></span><br><span class="line"><span class="comment">//greendao_step3:</span></span><br><span class="line">apply plugin: <span class="string">&#x27;org.greenrobot.greendao&#x27;</span> </span><br><span class="line"></span><br><span class="line">android &#123;</span><br><span class="line">        ...</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//greendao_step4:</span></span><br><span class="line"><span class="comment">//GreenDao 3.0+采用注解的方式来定义实体类，通过gradle插件生成相应的代码</span></span><br><span class="line">greendao &#123;</span><br><span class="line">    schemaVersion <span class="number">1</span></span><br><span class="line">    daoPackage <span class="string">&#x27;com.cupster.func_db_greendao.auto&#x27;</span><span class="comment">//建议同一包名下</span></span><br><span class="line">    targetGenDir <span class="string">&#x27;src/main/java&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">dependencies</span> &#123;</span><br><span class="line">    implementation <span class="keyword">fileTree</span>(dir: <span class="string">&quot;libs&quot;</span>, <span class="keyword">include</span>: [<span class="string">&quot;*.jar&quot;</span>])</span><br><span class="line">    implementation <span class="string">&#x27;androidx.appcompat:appcompat:1.1.0&#x27;</span></span><br><span class="line">    implementation <span class="string">&#x27;androidx.constraintlayout:constraintlayout:1.1.3&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//greendao_step5: add library</span></span><br><span class="line">    implementation <span class="string">&#x27;org.greenrobot:greendao:3.3.0&#x27;</span> </span><br><span class="line">&#125;</span><br></pre>

<h2 id="2-3-混淆"><a href="#2-3-混淆" class="headerlink" title="2.3 混淆"></a>2.3 混淆</h2><p>step6:</p>
<pre class="highlight"><span class="line"># greendao_step6:</span><br><span class="line"># greendao <span class="number">3.3</span>.<span class="number">0</span> proguard</span><br><span class="line">#======================================================================</span><br><span class="line">-keepclassmembers <span class="keyword">class</span> * <span class="keyword">extends</span> org.greenrobot.greendao.AbstractDao &#123;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> java.lang.String TABLENAME;</span><br><span class="line">&#125;</span><br><span class="line">-keep <span class="keyword">class</span> **$Properties &#123; *; &#125;</span><br><span class="line"></span><br><span class="line"># <span class="keyword">If</span> you <span class="keyword">DO</span> use SQLCipher:</span><br><span class="line">-keep <span class="keyword">class</span> org.greenrobot.greendao.database.SqlCipherEncryptedHelper &#123; *; &#125;</span><br><span class="line"></span><br><span class="line"># <span class="keyword">If</span> you <span class="keyword">do</span> NOT use SQLCipher:</span><br><span class="line">-dontwarn net.sqlcipher.database.**</span><br><span class="line"># <span class="keyword">If</span> you <span class="keyword">do</span> NOT use RxJava:</span><br><span class="line">-dontwarn rx.**</span><br><span class="line">#======================================================================</span><br></pre>

<h2 id="2-4-编写实体类（设计表）"><a href="#2-4-编写实体类（设计表）" class="headerlink" title="2.4 编写实体类（设计表）"></a>2.4 编写实体类（设计表）</h2><p>step7:</p>
<p>编写实体类，根据需求加上响应注解，build project编译一下项目，就会自动生成中间代码。</p>
<p>在此之前需要简单了解一些基础的注解：</p>
<table>
<thead>
<tr>
<th>注解</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>@Entity</td>
<td>声明当前实体类为greendao实体类，相当于数据库中的表</td>
</tr>
<tr>
<td>@Id</td>
<td>@Id(autoincrement = true) 设置主键，自增长Id</td>
</tr>
<tr>
<td>@NotNull</td>
<td>声明该字段在数据库不能为空 NOT NULL</td>
</tr>
</tbody></table>
<pre class="highlight"><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Id(autoincrement = true)</span></span><br><span class="line">    <span class="keyword">private</span> Long _id;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> dr;</span><br><span class="line">    <span class="keyword">private</span> String headUrl;</span><br><span class="line">    <span class="keyword">private</span> String obj;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@NotNull</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> String auth_token;</span><br><span class="line">    <span class="meta">@NotNull</span></span><br><span class="line">    <span class="keyword">private</span> String account;</span><br><span class="line">    <span class="meta">@NotNull</span></span><br><span class="line">    <span class="keyword">private</span> String password;</span><br><span class="line"></span><br></pre>

<h2 id="2-5编写DaoManager"><a href="#2-5编写DaoManager" class="headerlink" title="2.5编写DaoManager"></a>2.5编写DaoManager</h2><p>step8:</p>
<p>build project之后，会在目标文件夹，生成DaoMaster、DaoSession、XxxDao中间代码。</p>
<p>其中的意义如下：<br>|类 | 作用|<br>|—-|—|<br>| DaoMaster |DaoMaster保存数据库对象，代表着一个数据库的连接，内部有SQLiteOpenHelper实现 |<br>| DaoSession | 为实体提供了一些通用的持久性方法，如CURD |<br>| XxxDAO | [表名Dao]数据库表的访问对象，拥有基本的CURD及统计类等高级方法 |</p>
<p>使用之前，需要我们先编写DaoManager,封装数据库的创建、表创建、增删改查，以及数据库的升级等</p>
<pre class="highlight"><span class="line"><span class="comment">//greendao_step8:编写dao管理类</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DaoManager</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">&quot;DaoManager&quot;</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String DB_NAME =<span class="string">&quot;demo_db&quot;</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> SHOW_LOG = <span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Context mContext;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">static</span> DaoManager mDaoManager = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> DaoMaster mDaoMaster = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> DaoMaster.DevOpenHelper mDaoMasterHelper = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> DaoSession mDaoSession ;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> DaoManager <span class="title">getInstance</span><span class="params">(Context appContext)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mDaoManager == <span class="keyword">null</span>)&#123;</span><br><span class="line">            mDaoManager = <span class="keyword">new</span> DaoManager(appContext);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span>  mDaoManager;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="title">DaoManager</span><span class="params">(Context context)</span></span>&#123;</span><br><span class="line">        mContext = context;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> DaoMaster <span class="title">getDaoMaster</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mDaoMaster == <span class="keyword">null</span>)&#123;</span><br><span class="line">            mDaoMasterHelper = <span class="keyword">new</span> DaoMaster.DevOpenHelper(mContext ,DB_NAME ,<span class="keyword">null</span>);</span><br><span class="line">            mDaoMaster = <span class="keyword">new</span> DaoMaster(mDaoMasterHelper.getWritableDb());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> mDaoMaster;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> DaoSession <span class="title">getDaoSession</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mDaoSession == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span> (mDaoMaster == <span class="keyword">null</span> )&#123;</span><br><span class="line">                getDaoMaster();</span><br><span class="line">            &#125;</span><br><span class="line">            mDaoSession = mDaoMaster.newSession();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> mDaoSession;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">debug</span><span class="params">()</span></span>&#123;</span><br><span class="line">        QueryBuilder.LOG_SQL = <span class="keyword">true</span>;</span><br><span class="line">        QueryBuilder.LOG_VALUES = <span class="keyword">true</span>;</span><br><span class="line">        SHOW_LOG = <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">closeConnection</span><span class="params">()</span></span>&#123;</span><br><span class="line">        closeHelper();</span><br><span class="line">        clearDaoSession();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">closeHelper</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mDaoMasterHelper != <span class="keyword">null</span>)&#123;</span><br><span class="line">            mDaoMasterHelper.close();</span><br><span class="line">            mDaoMasterHelper = <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">clearDaoSession</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (mDaoSession != <span class="keyword">null</span>)&#123;</span><br><span class="line">            mDaoSession.clear();</span><br><span class="line">            mDaoSession = <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>

<h2 id="2-6编写XxxDaoHelper"><a href="#2-6编写XxxDaoHelper" class="headerlink" title="2.6编写XxxDaoHelper"></a>2.6编写XxxDaoHelper</h2><p>编写操作封装类XxxDaoHelper，用于完成具体的CRUD、count、条件查询操作、原始sql语句查询等</p>
<p>以用户表为例：</p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.func_db_greendao.crud;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.content.Context;</span><br><span class="line"><span class="keyword">import</span> android.util.Log;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.cupster.func_db_greendao.DaoManager;</span><br><span class="line"><span class="keyword">import</span> com.cupster.func_db_greendao.auto.UserDao;</span><br><span class="line"><span class="keyword">import</span> com.cupster.func_db_greendao.entity.User;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.greenrobot.greendao.query.QueryBuilder;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserDaoHelper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">&quot;UserDaoHelper&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> DaoManager mManager;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">UserDaoHelper</span><span class="params">(Context appContext)</span></span>&#123;</span><br><span class="line">        mManager = DaoManager.getInstance( appContext );</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 单条插入</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> user</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">insert</span><span class="params">(User user)</span></span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> result = mManager.getDaoSession().getUserDao().insert(user) != -<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span> (DaoManager.SHOW_LOG)&#123;</span><br><span class="line">            Log.d(TAG, <span class="string">&quot;insert: &quot;</span> + result +<span class="string">&quot; ==&gt; &quot;</span>+ user.toString());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 多条item插入</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> userList</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> boolean结果</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">insertMulti</span><span class="params">(<span class="keyword">final</span> List&lt;User&gt; userList)</span></span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> result = <span class="keyword">false</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            mManager.getDaoSession().runInTx(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                    <span class="keyword">for</span> (User user : userList)&#123;</span><br><span class="line">                       <span class="keyword">long</span> add_id = mManager.getDaoSession().insertOrReplace(user) ;</span><br><span class="line">                        <span class="keyword">if</span> (DaoManager.SHOW_LOG)&#123;</span><br><span class="line">                            Log.d(TAG, <span class="string">&quot;insert items : &quot;</span> + add_id +<span class="string">&quot; ==&gt; &quot;</span>+ user.toString());</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception e)&#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">update</span><span class="params">(User user)</span></span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> result = <span class="keyword">false</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            mManager.getDaoSession().update(user);</span><br><span class="line">            result = <span class="keyword">true</span>;</span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception e)&#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (DaoManager.SHOW_LOG)&#123;</span><br><span class="line">            Log.d(TAG, <span class="string">&quot;update: &quot;</span> + result +<span class="string">&quot; ==&gt; &quot;</span>+ user.toString());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">delete</span><span class="params">(User user)</span></span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> result = <span class="keyword">false</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            mManager.getDaoSession().delete(user);</span><br><span class="line">            result = <span class="keyword">true</span>;</span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception e )&#123;e.printStackTrace();&#125;</span><br><span class="line">        <span class="keyword">if</span> (DaoManager.SHOW_LOG)&#123;</span><br><span class="line">            Log.d(TAG, <span class="string">&quot;delete: &quot;</span> + result +<span class="string">&quot; ==&gt; &quot;</span>+ user.toString());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">deleteAll</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> result = <span class="keyword">false</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            mManager.getDaoSession().deleteAll(User.class);</span><br><span class="line">            result = <span class="keyword">true</span>;</span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception e )&#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (DaoManager.SHOW_LOG)&#123;</span><br><span class="line">            Log.d(TAG, <span class="string">&quot;delete all : &quot;</span> + result );</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span>  List &lt;User&gt; queryAll ()&#123;</span><br><span class="line">        <span class="keyword">return</span> mManager.getDaoSession().loadAll(User.class);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> User <span class="title">queryById</span><span class="params">(<span class="keyword">long</span> _id)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> mManager.getDaoSession().load(User.class ,_id);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">queryBySQL</span><span class="params">(String sql , String[] conditions)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> mManager.getDaoSession().queryRaw(User.class ,sql ,conditions);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用queryBuilder 条件查询</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">queryByQueryBuilder</span><span class="params">(<span class="keyword">long</span> id)</span></span>&#123;</span><br><span class="line">        QueryBuilder&lt;User&gt; queryBuilder = mManager.getDaoSession().queryBuilder(User.class);</span><br><span class="line">        <span class="keyword">return</span> queryBuilder.where(UserDao.Properties._id.eq(id)).list();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用queryBuilder 条件查询</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">queryByQueryBuilder</span><span class="params">(String account)</span></span>&#123;</span><br><span class="line">        QueryBuilder&lt;User&gt; queryBuilder = mManager.getDaoSession().queryBuilder(User.class);</span><br><span class="line">        <span class="keyword">return</span> queryBuilder.where(UserDao.Properties.Account.eq(account)).list();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> isDelete 0=false 1=true</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">queryByQueryBuilder</span><span class="params">(<span class="keyword">int</span> isDelete)</span></span>&#123;</span><br><span class="line">        QueryBuilder&lt;User&gt; queryBuilder = mManager.getDaoSession().queryBuilder(User.class);</span><br><span class="line">        <span class="keyword">return</span> queryBuilder.where(UserDao.Properties.Dr.eq(isDelete)).list();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>


<p>至此，我们已经完成了具体表的操作封装。</p>
<h1 id="3-具体使用"><a href="#3-具体使用" class="headerlink" title="3. 具体使用"></a>3. 具体使用</h1><p>实际结果：</p>
<img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210301165213_数据库增删改查.gif" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210301165213_数据库增删改查.gif" class="lozad post-image">

<p>初始代码：</p>
<pre class="highlight"><span class="line"></span><br><span class="line">TextView mTvShow;</span><br><span class="line">UserDaoHelper mUserDaoHelper;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">    setContentView(R.layout.activity_main);</span><br><span class="line">    findViewById(R.id.btn_add_item).setOnClickListener(<span class="keyword">this</span>);</span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">    mTvShow = findViewById(R.id.tv_show);</span><br><span class="line">    mUserDaoHelper = <span class="keyword">new</span> UserDaoHelper(getApplicationContext());</span><br><span class="line">&#125;</span><br></pre>

<h2 id="3-1增删改查-增-Insert"><a href="#3-1增删改查-增-Insert" class="headerlink" title="3.1增删改查-增 Insert"></a>3.1增删改查-增 Insert</h2><p>基于以上代码，进行单条数据插入：</p>
<img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210301165837_insert_1.gif" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210301165837_insert_1.gif" class="lozad post-image">

<pre class="highlight"><span class="line">User user = <span class="keyword">new</span> User();</span><br><span class="line">user.setAccount(<span class="string">&quot;18960222222&quot;</span>);</span><br><span class="line">user.setName(<span class="string">&quot;Cupster&quot;</span>);</span><br><span class="line">user.setPassword(<span class="string">&quot;Cupster@163.com&quot;</span>);</span><br><span class="line"><span class="keyword">if</span> (mUserDaoHelper.insert(user))&#123;</span><br><span class="line">    Toast.makeText(<span class="keyword">this</span> ,<span class="string">&quot;已添加&quot;</span>,Toast.LENGTH_SHORT).show();</span><br><span class="line">&#125;</span><br></pre>

<p>list数据插入：</p>
<img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210301165213_数据库增删改查.gif" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210301165213_数据库增删改查.gif" class="lozad post-image">
<pre class="highlight"><span class="line">List&lt;User&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">User user1 = <span class="keyword">new</span> User();</span><br><span class="line">user1.setAccount(<span class="string">&quot;18960333333&quot;</span>);</span><br><span class="line">user1.setName(<span class="string">&quot;CupsterXX&quot;</span>);</span><br><span class="line">user1.setPassword(<span class="string">&quot;Cupster@163.com&quot;</span>);</span><br><span class="line">User user2 = <span class="keyword">new</span> User();</span><br><span class="line">user2.setAccount(<span class="string">&quot;18960444444&quot;</span>);</span><br><span class="line">user2.setName(<span class="string">&quot;Div&quot;</span>);</span><br><span class="line">user2.setDr(<span class="number">1</span>);</span><br><span class="line">user2.setPassword(<span class="string">&quot;Cupster2@163.com&quot;</span>);</span><br><span class="line">User user3 = <span class="keyword">new</span> User();</span><br><span class="line">user3.setAccount(<span class="string">&quot;18960555555&quot;</span>);</span><br><span class="line">user3.setName(<span class="string">&quot;Higloo&quot;</span>);</span><br><span class="line">user3.setPassword(<span class="string">&quot;Cupster3@163.com&quot;</span>);</span><br><span class="line">list.add(user1);</span><br><span class="line">list.add(user2);</span><br><span class="line">list.add(user3);</span><br><span class="line"><span class="keyword">if</span> (mUserDaoHelper.insertMulti(list))&#123;</span><br><span class="line">    Toast.makeText(<span class="keyword">this</span> ,<span class="string">&quot;已添加&quot;</span>,Toast.LENGTH_SHORT).show();</span><br><span class="line">&#125;</span><br></pre>

<h2 id="3-2增删改查-删-Delete"><a href="#3-2增删改查-删-Delete" class="headerlink" title="3.2增删改查-删 Delete"></a>3.2增删改查-删 Delete</h2><p>删除所有</p>
<img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210301170230_deletell.gif" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210301170230_deletell.gif" class="lozad post-image">

<pre class="highlight"><span class="line"><span class="keyword">if</span> (mUserDaoHelper.deleteAll())&#123;</span><br><span class="line">    Toast.makeText(<span class="keyword">this</span> ,<span class="string">&quot;已删除&quot;</span>,Toast.LENGTH_SHORT).show();</span><br><span class="line">&#125;</span><br></pre>

<p>删除指定数据</p>
<pre class="highlight"><span class="line">User toDelelte =<span class="keyword">null</span>;</span><br><span class="line">List &lt;User&gt; queryList = mUserDaoHelper.queryByQueryBuilder(<span class="string">&quot;18960222222&quot;</span>);</span><br><span class="line"><span class="keyword">if</span> (queryList != <span class="keyword">null</span> &amp;&amp; queryList.size() &gt; <span class="number">0</span> )&#123;</span><br><span class="line">    toDelelte = queryList.get(<span class="number">0</span>);</span><br><span class="line">    <span class="keyword">if</span> (mUserDaoHelper.delete(toDelelte))&#123;</span><br><span class="line">        Toast.makeText(<span class="keyword">this</span> ,<span class="string">&quot;已删除&quot;</span>,Toast.LENGTH_SHORT).show();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">    Toast.makeText(<span class="keyword">this</span> ,<span class="string">&quot;不存在or已删除&quot;</span>,Toast.LENGTH_SHORT).show();</span><br><span class="line">&#125;</span><br></pre>

<h2 id="3-3增删改查-改-Update"><a href="#3-3增删改查-改-Update" class="headerlink" title="3.3增删改查-改 Update"></a>3.3增删改查-改 Update</h2><p>修改指定数据</p>
<pre class="highlight"><span class="line">User toUpdate = <span class="keyword">null</span>;</span><br><span class="line">List &lt;User&gt; queryList2 = mUserDaoHelper.queryByQueryBuilder(<span class="string">&quot;18960222222&quot;</span>);</span><br><span class="line"><span class="keyword">if</span> (queryList2 != <span class="keyword">null</span> &amp;&amp; queryList2.size() &gt; <span class="number">0</span> )&#123;</span><br><span class="line">    toUpdate = queryList2.get(<span class="number">0</span>);</span><br><span class="line">    toUpdate.setName(toUpdate.getName() +<span class="string">&quot;[改]&quot;</span>);</span><br><span class="line">&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">    toUpdate = <span class="keyword">new</span> User();</span><br><span class="line">    toUpdate.setName(<span class="string">&quot;Xxx&quot;</span>);</span><br><span class="line">    toUpdate.setAccount(<span class="string">&quot;133333333333&quot;</span>);</span><br><span class="line">    toUpdate.setPassword(<span class="string">&quot;a123456&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span> (mUserDaoHelper.update(toUpdate))&#123;</span><br><span class="line">    Toast.makeText(<span class="keyword">this</span> ,<span class="string">&quot;已修改&quot;</span>,Toast.LENGTH_SHORT).show();</span><br><span class="line">&#125;</span><br></pre>

<h2 id="3-4增删改查-查-Query"><a href="#3-4增删改查-查-Query" class="headerlink" title="3.4增删改查-查 Query"></a>3.4增删改查-查 Query</h2><p>查询所有:</p>
<pre class="highlight"><span class="line">List&lt;User&gt; users = mUserDaoHelper.queryAll();</span><br><span class="line">StringBuilder strBuilder = <span class="keyword">new</span> StringBuilder();</span><br><span class="line"><span class="keyword">for</span> (User item : users) &#123;</span><br><span class="line">    strBuilder.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    strBuilder.append(item.get_id());</span><br><span class="line">    strBuilder.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    strBuilder.append(item.getName());</span><br><span class="line">    strBuilder.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    strBuilder.append(item.getAccount());</span><br><span class="line">&#125;</span><br><span class="line">mTvShow.setText(strBuilder.toString());</span><br></pre>

<p>查询指定id:</p>
<pre class="highlight"><span class="line">User userById = mUserDaoHelper.queryById(<span class="number">1</span>);</span><br><span class="line">StringBuilder strBuilder1 = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">strBuilder1.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">strBuilder1.append(<span class="string">&quot;查询id=0账户:&quot;</span>);</span><br><span class="line">strBuilder1.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">strBuilder1.append(userById.get_id());</span><br><span class="line">strBuilder1.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">strBuilder1.append(userById.getName());</span><br><span class="line">strBuilder1.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">strBuilder1.append(userById.getAccount());</span><br><span class="line">mTvShow.setText(strBuilder1.toString());</span><br></pre>

<p>sql语句查询：</p>
<pre class="highlight"><span class="line">String sql = <span class="string">&quot;where _id &gt; ?&quot;</span>;</span><br><span class="line">String[] condition = <span class="keyword">new</span> String[]&#123;<span class="string">&quot;0&quot;</span>&#125;;</span><br><span class="line">List&lt;User&gt; queryBySQL = mUserDaoHelper.queryBySQL(sql, condition);</span><br><span class="line">StringBuilder strBuilder2 = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">strBuilder2.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">strBuilder2.append(<span class="string">&quot;查询id&gt;0,账户:&quot;</span>);</span><br><span class="line"><span class="keyword">for</span> (User u : queryBySQL)&#123;</span><br><span class="line">    strBuilder2.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    strBuilder2.append(u.get_id());</span><br><span class="line">    strBuilder2.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    strBuilder2.append(u.getName());</span><br><span class="line">    strBuilder2.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    strBuilder2.append(u.getAccount());</span><br><span class="line">&#125;</span><br><span class="line">mTvShow.setText(strBuilder2.toString());</span><br></pre>

<p>条件查询：</p>
<pre class="highlight"><span class="line">List&lt;User&gt; userByWhereList = mUserDaoHelper.queryByQueryBuilder(<span class="number">1</span>);</span><br><span class="line">                StringBuilder strBuilder3 = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">                strBuilder3.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">                strBuilder3.append(<span class="string">&quot;查询dr=1,假删除账户:&quot;</span>);</span><br><span class="line">                <span class="keyword">for</span> (User u : userByWhereList)&#123;</span><br><span class="line">                    strBuilder3.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">                    strBuilder3.append(u.get_id());</span><br><span class="line">                    strBuilder3.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">                    strBuilder3.append(u.getName());</span><br><span class="line">                    strBuilder3.append(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">                    strBuilder3.append(u.getAccount());</span><br><span class="line">                &#125;</span><br><span class="line">                mTvShow.setText(strBuilder3.toString());</span><br></pre>


<h1 id="4-总结"><a href="#4-总结" class="headerlink" title="4.总结"></a>4.总结</h1><p>至此，从配置到增删改查已简述完毕，具体代码请参考：</p>
<p><a target="_blank" rel="noopener" href="https://download.csdn.net/download/Cupster/15503301">GreenDao3.3.0Demo.zip</a></p>
<p><a target="_blank" rel="noopener" href="https://gitee.com/cupster/greendaodemo.git">Gitee: GreenDao3.3.0Demo</a></p>

  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2019/10/12/031-Android%E5%AD%98%E5%82%A8%EF%BC%886%EF%BC%89%EF%BC%9AGreenDao%E4%BD%BF%E7%94%A8%E5%85%A8%E8%A7%A3/">http://example.com/2019/10/12/031-Android%E5%AD%98%E5%82%A8%EF%BC%886%EF%BC%89%EF%BC%9AGreenDao%E4%BD%BF%E7%94%A8%E5%85%A8%E8%A7%A3/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2019/10/16/032-Android存储（7）：LitePal使用详解/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">032-Android存储（7）：LitePal使用详解 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2019/10/07/030-Android存储（5）：数据库sqlite简介及框架选择分析/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">030-Android存储（5）：数据库sqlite简介及框架选择分析 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8"><span class="toc-text">2.使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E9%85%8D%E7%BD%AE"><span class="toc-text">2.1 配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%BA%94%E7%94%A8%E6%8F%92%E4%BB%B6-amp-%E5%AF%BC%E5%8C%85"><span class="toc-text">2.2 应用插件&amp;导包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E6%B7%B7%E6%B7%86"><span class="toc-text">2.3 混淆</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E7%BC%96%E5%86%99%E5%AE%9E%E4%BD%93%E7%B1%BB%EF%BC%88%E8%AE%BE%E8%AE%A1%E8%A1%A8%EF%BC%89"><span class="toc-text">2.4 编写实体类（设计表）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5%E7%BC%96%E5%86%99DaoManager"><span class="toc-text">2.5编写DaoManager</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-6%E7%BC%96%E5%86%99XxxDaoHelper"><span class="toc-text">2.6编写XxxDaoHelper</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%85%B7%E4%BD%93%E4%BD%BF%E7%94%A8"><span class="toc-text">3. 具体使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E5%A2%9E-Insert"><span class="toc-text">3.1增删改查-增 Insert</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E5%88%A0-Delete"><span class="toc-text">3.2增删改查-删 Delete</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E6%94%B9-Update"><span class="toc-text">3.3增删改查-改 Update</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E6%9F%A5-Query"><span class="toc-text">3.4增删改查-查 Query</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%80%BB%E7%BB%93"><span class="toc-text">4.总结</span></a></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8"><span class="toc-text">2.使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E9%85%8D%E7%BD%AE"><span class="toc-text">2.1 配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%BA%94%E7%94%A8%E6%8F%92%E4%BB%B6-amp-%E5%AF%BC%E5%8C%85"><span class="toc-text">2.2 应用插件&amp;导包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E6%B7%B7%E6%B7%86"><span class="toc-text">2.3 混淆</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E7%BC%96%E5%86%99%E5%AE%9E%E4%BD%93%E7%B1%BB%EF%BC%88%E8%AE%BE%E8%AE%A1%E8%A1%A8%EF%BC%89"><span class="toc-text">2.4 编写实体类（设计表）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5%E7%BC%96%E5%86%99DaoManager"><span class="toc-text">2.5编写DaoManager</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-6%E7%BC%96%E5%86%99XxxDaoHelper"><span class="toc-text">2.6编写XxxDaoHelper</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%85%B7%E4%BD%93%E4%BD%BF%E7%94%A8"><span class="toc-text">3. 具体使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E5%A2%9E-Insert"><span class="toc-text">3.1增删改查-增 Insert</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E5%88%A0-Delete"><span class="toc-text">3.2增删改查-删 Delete</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E6%94%B9-Update"><span class="toc-text">3.3增删改查-改 Update</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E6%9F%A5-Query"><span class="toc-text">3.4增删改查-查 Query</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%80%BB%E7%BB%93"><span class="toc-text">4.总结</span></a></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8"><span class="toc-text">2.使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E9%85%8D%E7%BD%AE"><span class="toc-text">2.1 配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%BA%94%E7%94%A8%E6%8F%92%E4%BB%B6-amp-%E5%AF%BC%E5%8C%85"><span class="toc-text">2.2 应用插件&amp;导包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E6%B7%B7%E6%B7%86"><span class="toc-text">2.3 混淆</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E7%BC%96%E5%86%99%E5%AE%9E%E4%BD%93%E7%B1%BB%EF%BC%88%E8%AE%BE%E8%AE%A1%E8%A1%A8%EF%BC%89"><span class="toc-text">2.4 编写实体类（设计表）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5%E7%BC%96%E5%86%99DaoManager"><span class="toc-text">2.5编写DaoManager</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-6%E7%BC%96%E5%86%99XxxDaoHelper"><span class="toc-text">2.6编写XxxDaoHelper</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%85%B7%E4%BD%93%E4%BD%BF%E7%94%A8"><span class="toc-text">3. 具体使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E5%A2%9E-Insert"><span class="toc-text">3.1增删改查-增 Insert</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E5%88%A0-Delete"><span class="toc-text">3.2增删改查-删 Delete</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E6%94%B9-Update"><span class="toc-text">3.3增删改查-改 Update</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5-%E6%9F%A5-Query"><span class="toc-text">3.4增删改查-查 Query</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%80%BB%E7%BB%93"><span class="toc-text">4.总结</span></a></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>